<?php
class News extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('news_model');
	}

	public function index()
	{
		$data['news'] = $this->news_model->get_news();
		$data['title'] = 'News archive';

		$this->load->view('templates/header', $data);
		$this->load->view('news/index', $data);
		$this->load->view('templates/footer');
	}

	public function view($slug)
	{
		$data['news_item'] = $this->news_model->get_news($slug);

		if (empty($data['news_item']))
		{
			show_404();
		}

		$data['title'] = $data['news_item']['title'];

		$this->load->view('templates/header', $data);
		$this->load->view('news/view', $data);
		$this->load->view('templates/footer');
	}
	
	public function get_news($slug=''){
		if (isset($slug)&& $slug!=""){
			$result=$this->news_model->get_news($slug);
		} else{
			$result=$this->news_model->get_news();
		}
		$this->output->set_content_type('application/json')->set_output(json_encode($result));	
	}
	
	public function news_register($idusuario_w='',$monto_w='',$fecha_w='',$numvoucher_w=''){
		$idusuario=$this->input->post('idusuario');
		$monto=$this->input->post('monto');
		$fecha=$this->input->post('fecha');
		$numvoucher=$this->input->post('numvoucher');
		if ($idusuario_w!='' && $monto_w!='' && $fecha_w!='' && $numvoucher_w!=''){
			$idusuario=$idusuario_w;
			$monto=$monto_w;
			$fecha=$fecha_w;
			$numvoucher=$numvoucher_w;
		}
		$result=$this->Payment_model->registro_deposito($idusuario,$monto, $fecha, $numvoucher);		
		$this->output->set_content_type('application/json')->set_output($result);				
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$data['title'] = 'Create a news item';

		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('text', 'text', 'required');

		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('templates/header', $data);
			$this->load->view('news/create');
			$this->load->view('templates/footer');

		}
		else
		{
			$this->news_model->set_news();
			$this->load->view('news/success');
		}
	}
}